    package com.share.payment.api;

    import com.share.common.core.domain.R;
    import com.share.payment.service.IAliPayService;
    import io.swagger.v3.oas.annotations.Operation;
    import io.swagger.v3.oas.annotations.tags.Tag;
    import jakarta.servlet.http.HttpServletRequest;
    import lombok.Data;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;


    import java.util.HashMap;
    import java.util.Map;

    /**
     * 支付宝支付Controller
     *
     * @author share
     * @date 2024-04-16
     */
    @Slf4j
    @Tag(name = "支付宝支付接口管理")
    @RestController
    @RequestMapping("/aliPay")
    public class AliPayApiController {

        @Autowired
        private IAliPayService aliPayService;

        @Data
        public static class CreatePayRequest {
            private String orderNo;
            private String amount;
            private String subject;
        }

        /**
         * 创建支付
         */
        @Operation(summary = "创建支付")
        @PostMapping("/createPay")
        public R<String> createPay(@RequestBody CreatePayRequest request) {
            String payForm = aliPayService.createPay(request.getOrderNo(), request.getAmount(), request.getSubject());
            if (payForm == null) {
                return R.fail("创建支付失败");
            }
            return R.ok(payForm);
        }

        /**
         * 查询支付状态
         */
        @Operation(summary = "查询支付状态")
        @GetMapping("/queryPayStatus/{orderNo}")
        public R<String> queryPayStatus(@PathVariable String orderNo) {
            String status = aliPayService.queryPayStatus(orderNo);
            return R.ok(status);
        }

        /**
         * 支付回调
         */
        @Operation(summary = "支付回调")
        @PostMapping("/notify")
        public String notify(HttpServletRequest request) {
            // 1. 获取回调参数
            Map<String, String> params = new HashMap<>();
            Map<String, String[]> requestParams = request.getParameterMap();
            for (String name : requestParams.keySet()) {
                String[] values = requestParams.get(name);
                String valueStr = "";
                for (int i = 0; i < values.length; i++) {
                    valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
                }
                params.put(name, valueStr);
            }

            // 2. 处理支付回调
            boolean result = aliPayService.handlePayNotify(params);
            if (result) {
                return "success";
            } else {
                return "fail";
            }
        }

        /**
         * 支付返回
         */
        @Operation(summary = "支付返回")
        @GetMapping("/return")
        public String returnUrl(HttpServletRequest request) {
            // 1. 获取返回参数
            Map<String, String> params = new HashMap<>();
            Map<String, String[]> requestParams = request.getParameterMap();
            for (String name : requestParams.keySet()) {
                String[] values = requestParams.get(name);
                String valueStr = "";
                for (int i = 0; i < values.length; i++) {
                    valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
                }
                params.put(name, valueStr);
            }

            // 2. 获取订单号
            String orderNo = params.get("out_trade_no");
            if (orderNo == null) {
                return "订单号为空";
            }

            // 3. 查询支付状态
            String status = aliPayService.queryPayStatus(orderNo);
            if ("已支付".equals(status)) {
                return "支付成功";
            } else {
                return "支付失败：" + status;
            }
        }
    } 